package com.share.function.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.share.function.bean.UserBean;
import com.share.function.service.LoginService;

//加上Controller注解，表示正式成为Controller
@Controller
public class LoginController {
	// loginService需要从IOC容器中查找
	@Autowired
	private LoginService loginService;

	/**
	 * 登录逻辑
	 * 
	 * @param loginUser
	 * @param session
	 * @return
	 */
	@RequestMapping("/login/login.action")
	// 传入实体类为Spring自动提取页面数据设置好的
	public ModelAndView login(UserBean loginUser, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("LoginController.loginUser.password.md5--" + loginUser.getsPassword());
		// 从Service层获取参数
		Map<String, Object> map = loginService.getMap(loginUser, request);
		// 设置自动登录
		loginService.setAutoLogin(request, map, response);
		// 设置下一个页面的去向
		ModelAndView mav = new ModelAndView((String) map.get("nextUrl"));
		// 设置传给下一个页面的参数
		mav.addAllObjects(map);
		// 设置session
		session.setAttribute("loginUser", map.get("loginUser"));
		return mav;
	}

	/**
	 * 得到登录信息逻辑
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("login/getLoginMsg.action")
	public ModelAndView loginMsg(HttpSession session, HttpServletRequest request) {
		System.out.println("进入getLoginMsg.action");
		UserBean loginUser = (UserBean) session.getAttribute("loginUser");
		System.out.println("getLoginMsg.action.page--" + request.getParameter("page"));
		if (null != request.getParameter("order") && null != request.getParameter("desc")) {
			loginUser.setOrder(request.getParameter("order"));
			loginUser.setDesc(request.getParameter("desc"));
		}
		loginUser.setPage(request.getParameter("page"));
		Map<String, Object> map = loginService.getLoginMsgMap(loginUser);
		ModelAndView mav = new ModelAndView((String) map.get("nextUrl"));
		mav.addAllObjects(map);
		return mav;
	}
}
